<?php
session_start();
require_once(LIBPATH.'sql.php');

class tableSection extends table
{
    public function tableSection()
    {
        parent:: table('section');
        $this->addColumn('sectionId' , 'varchar(255) PRIMARY KEY');
        $this->addColumn('name'      , 'varchar(255)');
        $this->addColumn('component' , 'varchar(255) NOT NULL');
        $this->addColumn('args'      , 'varchar(255)');
        $this->addColumn('isActive'  , 'boolean');
        $this->addColumn('level'     , 'integer');
        $result = $this->db->query( $this->create() );
    }

    public function insertSection($data)
    {
        if ( !$data['sectionId'] || !$data['component'] )
        {
            return false;
        }

        $section = $this->getSection( $data['sectionId'] );

        if ( $section['sectionId'] )
        {
            return $this->updateSection($data);
        }
        else
        {
            return $this->autoInsert($data);
        }
    }

    public function updateSection($data)
    {
		$this->clear();
		$this->addWhere('sectionId=', $data['sectionId']);
		$sql = parent::update($data);
        return $this->db->exec($sql);
    }

    public function listAll()
    {
        $this->clearWhere();
        $sql = $this->select();
        $result = $this->db->query($sql);
        $this->setOrderBy('level');
        return $result;
    }

    public function listActive($onlyActive = true)
    {
        if ($onlyActive)
        {
            $data['isActive'] = '1';
        }
        $this->setOrderBy('level');
        $result = $this->autoSelect($data);
        return  $result;
    }

    public function getSection($section)
    {
        $data['sectionId'] = $section;
        $result = $this->autoSelect($data);
        return  $result[0];
    }
}
?>
